package com.miracle9.game.dao;

import com.miracle9.game.entity.WaterDesk;

import java.io.PrintStream;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

@Repository
public class WaterDeskServiceDao extends SqlOperating<WaterDesk, Integer> {

    public List<Integer> getDeskIdsByRoomId(int roomId){
        return (List<Integer>)getSession().createQuery("select id from WaterDesk where roomId = ?").setParameter(0, roomId).list();
    }

    public WaterDesk getWaterDeskByDeskId(int deskId) {
        return (WaterDesk) getById(Integer.valueOf(deskId));
    }

    public boolean updateGameSumByDeskId(int deskId) {
        int nCount = q_CreateSql("update WaterDesk set mainGameSumYaFen=0,mainGameSumDeFen=0 ,diceGameSumYaFen=0,diceGameSumDeFen=0 ,sumYaFen = 0,sumDeFen = 0 where id=?", new Object[]{
                Integer.valueOf(deskId)}).executeUpdate();
        return nCount > 0;
    }

    public List<WaterDesk> getWaterDesks() {
        return q_CreateSqlList("from WaterDesk  order by roomId desc,orderBy", new Object[0]);
    }

    public void updateStateById(int id, int state) {
        q_CreateSql("update WaterDesk set state=? where id=?", new Object[]{Integer.valueOf(state), Integer.valueOf(id)}).executeUpdate();
    }

    public void updateOrderByById(int id, int index) {
        int n = q_CreateSql("update WaterDesk set orderBy=? where id=?", new Object[]{Integer.valueOf(index), Integer.valueOf(id)}).executeUpdate();
        if (n > 0) {
            System.out.println("更新桌子排序字段成功");
        } else {
            System.out.println("更新桌子排序字段不....成功");
        }
    }

    public boolean getQuantityByDeskName(String deskName) {
        long num = 0L;
        num = q_CreateSqlInt("SELECT COUNT(*)  FROM WaterDesk wd WHERE wd.name = ? ", new Object[]{deskName});
        if (num == 0L) {
            return false;
        }
        return true;
    }

    public boolean getQuantityByDeskNameAndDeskId(String deskName, int deskId) {
        int num = 0;
        num = q_CreateSqlInt("SELECT COUNT(*)  FROM WaterDesk wd WHERE wd.name = ? AND wd.id != ?", new Object[]{deskName, Integer.valueOf(deskId)});
        if (num == 0) {
            return false;
        }
        return true;
    }

}
